﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using System.Xml.Linq;

namespace EasyXaf.LowCode.WorkflowEditors.Models.Activities;

[DomainComponent]
[ImageName("WF_Decision")]
[XafDisplayName("判断(If)")]
[ActivityOrder(10)]
[ActivityGroup(ActivityGroupConsts.CommonGroupName)]
public class DecisionActivity : BranchesActivity
{
    public override string Color => "#D35400";

    public override string ImageName => "WF_Decision";

    public override bool HasDetailView => true;

    [XafDisplayName("条件")]
    public string Condition
    {
        get => GetPropertyValue<string>();
        set => SetPropertyValue(value);
    }

    public DecisionActivity(IWorkflow workflow, ISequence sequence)
        : base(workflow, sequence)
    {
        Caption = "判断";
    }

    public override void OnCreated()
    {
        base.OnCreated();

        Branches.Add(new TrueBranch(Workflow, this).Created());
        Branches.Add(new FalseBranch(Workflow, this).Created());
    }

    public override void WriteStateToXml(XElement element)
    {
        base.WriteStateToXml(element);

        SetXmlValue(element, nameof(Condition), Condition);
    }

    public override void ReadStateFromXml(XElement element)
    {
        base.ReadStateFromXml(element);

        Condition = GetXmlValue<string>(element, nameof(Condition));
    }
}